home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.1 KB  |  114 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include <stdio.h>
  22. #include "class.h"
  23.  
  24. #define max(a,b) (a < b ? b : a)
  25.  
  26. #ifdef MALDEBUG
  27. #define Bzero(a,b)    { char *q=(a), *r=(b); \
  28.             if(maldbug) printf("bzeroing(0x%x,0x%x)\n",q,r); \
  29.             bzero(q,r);}
  30. #else
  31. #define Bzero(a,b)    bzero(a,b)
  32. #endif /* MALDEBUG */
  33.  
  34. extern    char    *gfmalloc();
  35. extern int maldbug;
  36.  
  37. static fcnTable eot = {EOTABLE};
  38.  
  39.     static classFcn *
  40. expand(maxSelector, curClass)
  41.     int        *maxSelector;
  42.     class    *curClass;
  43. {
  44.     fcnTable    *entry = NULL;
  45.     classFcn    *classFcns = NULL;
  46.  
  47.     if(curClass == NULL) {
  48.         fprintf(stderr,"Expand: expand called with null curClass");
  49.     return classFcns;
  50.     }
  51.     if(maxSelector == NULL) {
  52.         fprintf(stderr,"Expand: expand called with null maxSelector");
  53.     return classFcns;
  54.     }
  55.     /* update maxSelector from curClass->fcnTable */
  56.     for(entry = curClass->fcnTable;
  57.     entry && bcmp(entry, &eot, sizeof(fcnTable)) != 0 /* unequal */;
  58.     entry++)
  59.     {
  60.     *maxSelector = (int)max((*maxSelector), entry->selector);
  61.     }
  62.     /* get expanded classFcns so far */
  63.     if(curClass->super) {
  64.     classFcns = expand(maxSelector, curClass->super);
  65.     } else if(*maxSelector >= 0) {
  66.     if (classFcns = (classFcn *)gfmalloc((unsigned)(++(*maxSelector))*4)) {
  67. #ifdef MALDEBUG
  68.         if(*(char *)classFcns) {
  69.         printf("expand: onto non empty gfmalloc 0x%x\n",classFcns);
  70.         }
  71. #endif /* MALDEBUG */
  72.         Bzero(classFcns, (unsigned)(*maxSelector)*4);
  73.     }
  74.     else {
  75.         fprintf(stderr,
  76.              "Expand: insufficient memory for class %x\n", curClass);
  77.         return NULL;
  78.     }
  79.     }
  80.     if(classFcns == NULL) {
  81.     return classFcns;
  82.     }
  83.     /* fill in classFcns */
  84.     for(entry = curClass->fcnTable;
  85.     entry && bcmp(entry, &eot, sizeof(fcnTable)) != 0 /* unequal */;
  86.     entry++)
  87.     {
  88.     classFcns[entry->selector] = entry->function;
  89.     }
  90.     return classFcns;
  91. }   /* expand */
  92.  
  93.     /*
  94.      * Expand a inst's classFcns from its class list.
  95.      * return address of expanded inst.
  96.      */
  97.     inst *
  98. Expand(obj)
  99.     inst    *obj;
  100. {
  101.     classFcn    *expand();
  102.  
  103.     if(obj == NULL) {
  104.     return NULL;
  105.     }
  106.     if(obj->classFcns) {
  107.     return obj;
  108.     }
  109.     obj->nFcns = -1;
  110.     obj->classFcns = expand(&obj->nFcns, obj->myClass);
  111.     return obj;
  112. }   /* Expand */
  113.  
  114.